const lodash = require('lodash')
const xmljs = require('xml-js')

const callBackKeys = [
    /(onLoad=).*.?(")/g,
    /(onAction=).+?(")/g,
    /get.*?( )/g,
    /(onChange=).*?(")/g,
    /(loadImage=).*?(")/g
]
/**
 * 获取控件回调配置项和值
 * @param {string} xmlContent XML内容
 * @returns {[[],...]}
 * @example
 * // returns
 *  [['onLoad','exampleLoad'],...]
 */
function callbackKeyAndValue(xmlContent){
    let f1 = xmlContent
    let kvs = [];
    for(let i=0;i<callBackKeys.length;i++){
        let item = callBackKeys[i];
        let item_matchs = f1.match(item)
        kvs.push(item_matchs)
    }
    kvs = lodash.flatten(kvs)
    kvs = lodash.compact(kvs)
    kvs = lodash.map(kvs,o=>o.trim())
    kvs = lodash.uniq(kvs)
    let bar = lodash.map(kvs,o=>o.replace(/\"/g,""))
    bar = lodash.map(bar,o=>o.split('='))
    return bar;
}
function xml2json(xmlContent){
    return xmljs.xml2json(xmlContent)
}
function xml2js(xmlContent){
    return xmljs.xml2js(xmlContent)
}
module.exports ={
    callbackKeyAndValue,
    xml2json,
    xml2js
}